package com.pg.agent.engine.service.utils;


import org.springframework.beans.BeanUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * ObjectUtils
 *
 * @author huhuaping
 * @date 2022-11-11
 */
public class ObjectUtils extends cn.hutool.core.util.ObjectUtil {
    /**
     * 字符串数据处理
     */
    public static String valueAsStr(Object value) {
        if (value instanceof String) {
            return (String) value;
        } else if (value != null) {
            return value.toString();
        } else {
            return null;
        }
    }

    /**
     * 整型数据处理
     */
    public static Integer valueAsInt(Object value) {
        if (value instanceof Integer) {
            return (Integer) value;
        } else if (value instanceof Number) {
            return ((Number) value).intValue();
        } else if (value instanceof String) {
            if ("NaN".equals(value)) {
                return null;
            }
            return Integer.valueOf((String) value);
        } else if (value instanceof Boolean) {
            return ((Boolean) value) ? 1 : 0;
        } else {
            return null;
        }
    }

    /**
     * 无用对象置为空
     */
    public static void setEmpty(Object value) {
        if (ObjectUtils.isNotNull(value)) {
            value = null;
        }
    }

    /**
     * map转MultiValueMap
     *
     * @param parameters
     * @return
     */
    public static MultiValueMap<String, String> map2MultiValueMap(Map<String, String> parameters) {
        MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>(parameters.size());
        if (ObjectUtils.isNull(parameters)) {
            return paramMap;
        }
        for (String key : parameters.keySet()) {
            paramMap.add(key, String.valueOf(parameters.get(key)));
        }
        return paramMap;
    }

    /**
     * map 转成 string
     *
     * @param map
     * @return
     */
    public static String mapToString(Map<String, String> map) {
        SortedMap<String, String> sortedMap = new TreeMap(map);
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
            if (StringUtils.isBlank(entry.getValue())) {
                continue;
            }
            sb.append(entry.getKey()).append('=').append(entry.getValue()).append('&');
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.length() == 0 ? "" : sb.toString();
    }

    /**
     * 拷贝数据到自定类型
     *
     * @param source      拷贝源
     * @param targetClass 结果类型
     * @param <T>         结果类型
     * @return 拷贝结果
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @author dujiawen
     */
    public static <T> T beanCopy(Object source, Class<T> targetClass) throws IllegalAccessException, InstantiationException {
        T t = targetClass.newInstance();
        BeanUtils.copyProperties(source, t);
        return t;
    }

    /**
     * 如果数据为空，新建
     *
     * @param obj
     * @param clazz 结果类型
     * @param <T>   结果类型
     * @return 拷贝结果
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @author dujiawen
     */
    public static <T> T newIfNull(T obj, Class<T> clazz) throws IllegalAccessException, InstantiationException {
        if (obj != null) {
            return obj;
        }

        return clazz.newInstance();
    }
}
